package com.karry.yl.web.services;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.baseinfo.Organization;
import com.karry.yl.domain.healthdata.HealthDataItem;
import com.karry.yl.domain.services.Items;
import com.karry.yl.service.baseinfo.OrganizationService;
import com.karry.yl.service.healthdata.HealthDataItemService;
import com.karry.yl.service.services.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Administrator
 */
@Controller
@RequestMapping("/items")
public class ItemsController extends SuperController {


    @Autowired
    private ItemsService itemsService;
    @Autowired
    private HealthDataItemService healthDataItemService;
    @Autowired
    private OrganizationService organizationService;

    @RequestMapping
    public String listUI(){
        return "services/items/list";
    }

    @RequestMapping("/add")
    public String addUI(ModelMap mapMode){
        Organization organization = organizationService.selectById(currentUser.getOrgId());
        Integer nature = organization.getNature();
        if(nature==1){//公办
            mapMode.put("nature",1);
        }else if(nature==2){
            mapMode.put("nature",2);
        }
        return "services/items/add";
    }

    @RequestMapping("edit")
    public String editUI(Long id, ModelMap map){
        Items items = itemsService.selectById(id);
        map.put("items",items);
        //获取健康项目
        if(items.getType() == Items.TYPE_TJ){
            List<Long> healthDataItemIds = itemsService.selectHealthDataItem(id);
            map.put("healthDataItemIds",healthDataItemIds);
            List<HealthDataItem> healthDataItems2 = healthDataItemService.selectItemList();
            Map<String, List<HealthDataItem>> healthDataItemsMap = getHealthDataItemsMap(healthDataItems2);
            map.put("healthDataItemsMap",healthDataItemsMap);
        }
        Organization organization = organizationService.selectById(currentUser.getOrgId());
        Integer nature = organization.getNature();
        if(nature==1){//公办
            map.put("nature",1);
        }else  if(nature==2){
            map.put("nature",2);
        }
        return "services/items/edit";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<Items> itemsPage = itemsService.selectPage(new Page<Items>(page, limit),new EntityWrapper<Items>().eq("orgId",currentUser.getOrgId()).eq("deleted",false).orderBy("createdTime",false));
        return new LayTableResponse(itemsPage.getRecords(),itemsPage.getTotal());
    }

    @RequestMapping("/save")
    public String save(Items items,@RequestParam(name = "healthDataItemId",required = false) List<Long> healthDataItemIds){
        items.setOrgId(currentUser.getOrgId());
        itemsService.save(items,healthDataItemIds);
        return "redirect:/items";
    }

    @RequestMapping("/update")
    public String update(Items items,@RequestParam(name = "healthDataItemId",required = false) List<Long> healthDataItemIds){
        items.setOrgId(currentUser.getOrgId());
        itemsService.update(items,healthDataItemIds);
        return "redirect:/items";
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        itemsService.deleteBatchIds(ids);
        return "redirect:/items";
    }

    /**
     * 获取所有的 健康数据项
     * @return
     */
    @RequestMapping("/healthDataItems")
    @ResponseBody
    public JsonResponse healthDataItems(){
        List<HealthDataItem> healthDataItems = healthDataItemService.selectItemList();
        return JsonResponse.success(getHealthDataItemsMap(healthDataItems));
    }

    private Map<String,List<HealthDataItem>> getHealthDataItemsMap(List<HealthDataItem> healthDataItems){
        Map<String,List<HealthDataItem>> rsMap = new HashMap<>();
        for(HealthDataItem hdi : healthDataItems){
            String typeName = hdi.getTypeName();
            if(rsMap.containsKey(typeName)){
                rsMap.get(typeName).add(hdi);
            }else {
                List<HealthDataItem> subList = new ArrayList<>();
                subList.add(hdi);
                rsMap.put(typeName,subList);
            }
        }
        return rsMap;
    }

}
